#pragma once

#include "TimeObservation.h"

#include "NamedElement.h"

namespace uml {

const bool TimeObservation::firstEvent_default_ = true;
TimeObservation::TimeObservation(const QString& umlType)
    : Observation(umlType) {}

void TimeObservation::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    Observation::fromJson(json, errorList);
    event_ = JsonSerializable::create<NamedElement>(json.value("event"), errorList);

    firstEvent_ = json.value("firstEvent").toBool(firstEvent_default_);
}

void TimeObservation::toJson(QJsonObject& json) {
    Observation::toJson(json);
    if (event_) {
        QJsonObject event_json;
        event_->toJson(event_json);
        json.insert("event", event_json);
    }
    if (firstEvent_ != firstEvent_default_) {
        json.insert("firstEvent", firstEvent_);
    }
}

/// <summary>
/// event getter
/// The TimeObservation is determined by the entering or exiting of the event
/// Element during execution.
/// </summary>
shared_ptr<NamedElement> TimeObservation::event() const {
    return event_;
}

/// <summary>
/// event setter
/// The TimeObservation is determined by the entering or exiting of the event
/// Element during execution.
/// </summary>
void TimeObservation::setEvent(shared_ptr<NamedElement> value) {
    event_ = value;
}

/// <summary>
/// firstEvent getter
/// The value of firstEvent is related to the event. If firstEvent is true, then the
/// corresponding observation event is the first time instant the execution enters
/// the event Element. If firstEvent is false, then the corresponding observation
/// event is the time instant the execution exits the event Element.
/// </summary>
bool TimeObservation::firstEvent() const {
    return firstEvent_;
}

/// <summary>
/// firstEvent setter
/// The value of firstEvent is related to the event. If firstEvent is true, then the
/// corresponding observation event is the first time instant the execution enters
/// the event Element. If firstEvent is false, then the corresponding observation
/// event is the time instant the execution exits the event Element.
/// </summary>
void TimeObservation::setFirstEvent(bool value) {
    firstEvent_ = value;
}

} // namespace uml
